﻿using UnityEngine;

namespace UnityView
{
    public abstract class AbsDialog : UILayout
    {
        // 背景按钮
        public ButtonView BackgroundButtonView;

        // 是否开启区域外点击消失
        private bool _enableOutboundDismiss = false;
        public bool EnableOutboundDismiss
        {
            set
            {
                if (value)
                {
                    BackgroundButtonView.AddListener(Dismiss);
                }
                else
                {
                    BackgroundButtonView.OnClickListener.RemoveAllListeners();
                }
                _enableOutboundDismiss = value;
            }
            get
            {
                return _enableOutboundDismiss;
            }
        }

        public UIView ContentView { get; protected set; }

        public static float DefaultWidth = 480f;
        public static float DefaultHeight = 250f;

        private const string DefaultDialogName = "Dialog";

        protected AbsDialog() : this(DefaultWidth, DefaultHeight)
        {
        }

        protected AbsDialog(float width, float height)
        {
            Name = DefaultDialogName;

            AnchorAlignment = AnchorAlignment.TopLeft;

            RectFill(RectTransform);
            BackgroundButtonView = new ButtonView(this) { UIObject = { name = "Background View" } };
            RectFill(BackgroundButtonView);
            BackgroundButtonView.BackgroundColor = Color.clear;

            EnableOutboundDismiss = true;

            ContentView = new UIView(this, "Content View")
            {
                AnchorAlignment = AnchorAlignment.Center,
                Frame = new Frame(0, 0, width, height)
            };

            Visible = false;
        }

        public void Show()
        {
            Visible = true;
            OnShow();
        }

        public void Hide()
        {
            OnDismiss();
        }

        public void Dismiss()
        {
            Destroy();
            OnDismiss();
        }

        public virtual void OnShow()
        {
        }

        public virtual void OnDismiss()
        {
        }
    }
}
